package com.controller;

import com.entity.RbacManager;
import com.entity.ResponseEntity;
import com.service.RbacManagerService;
import com.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * 登录控制器
 *
 * @author a0112
 * @date 2022/04/17
 */
@RestController
public class LoginController {
    @Autowired
    private RbacManagerService rbacManagerService;

    /**
     * 登录方法
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link ResponseEntity}
     */
    @RequestMapping("/login")
    public ResponseEntity login(String username, String password){
        RbacManager rbacManager = new RbacManager();
        rbacManager.setAccount(username);
        rbacManager.setPassword(password);
        System.out.println("rbacManager = " + rbacManager);
        RbacManager manager = rbacManagerService.findByAccountAndPassword(username, password);
        if(manager != null){
            String token = JWTUtils.sign(username,password);
            manager.setToken(token);
            return new ResponseEntity(200,"登录成功",manager);
        }
        return ResponseEntity.FAIL;
    }
    @RequestMapping("/401")
    public ResponseEntity fail401(){
        return new ResponseEntity(401,"认证异常！",null);
    }
}
